<template>
	<view class="page">
		<scroll-view class="content-container" scroll-y>
			<!-- 活动封面 -->
			<view class="cover-section">
				<view class="cover-container">
					<view class="cover-upload" @click="chooseCover">
						<view v-if="activityData.cover" class="cover-preview">
							<image :src="activityData.cover" class="cover-image" mode="aspectFill" />
							<view class="cover-overlay">
								<image src="/static/svg/camera.svg" class="edit-icon" />
								<text class="edit-text">更换封面</text>
							</view>
						</view>
						<view v-else class="upload-placeholder">
							<view class="upload-icon-wrapper">
								<image src="/static/svg/camera-large.svg" class="camera-icon" />
							</view>
							<text class="upload-title">添加活动封面</text>
							<text class="upload-subtitle">展示你精彩活动的第一印象</text>
						</view>
					</view>
				</view>
			</view>

			<!-- 基本信息 -->
			<view class="form-card">
				<view class="card-header">
					<view class="header-icon">
						<image src="/static/svg/edit.svg" class="icon" />
					</view>
					<text class="card-title">基本信息</text>
				</view>
				
				<view class="form-item">
					<text class="label">活动标题 <text class="required">*</text></text>
					<view class="input-wrapper">
						<input v-model="activityData.title" placeholder="给你的活动起个响亮的名字" class="modern-input" maxlength="30" />
						<text class="char-count">{{ activityData.title.length }}/30</text>
					</view>
				</view>

				<view class="form-item">
					<text class="label">活动描述 <text class="required">*</text></text>
					<view class="textarea-wrapper">
						<textarea v-model="activityData.description" placeholder="详细描述你的活动内容，让更多人了解并参与..." class="modern-textarea" maxlength="200" />
						<text class="char-count">{{ activityData.description.length }}/200</text>
					</view>
				</view>

				<view class="form-item">
					<text class="label">活动分类 <text class="required">*</text></text>
					<picker :range="categories" range-key="name" @change="onCategoryChange">
						<view class="modern-picker">
							<view class="picker-content">
								<image v-if="selectedCategory.icon" :src="selectedCategory.icon" class="category-icon" />
								<text class="picker-text">{{ selectedCategory.name || '请选择活动分类' }}</text>
							</view>
							<image src="/static/svg/chevron-right.svg" class="arrow-icon" />
						</view>
					</picker>
				</view>
			</view>

			<!-- 时间地点 -->
			<view class="form-card">
				<view class="card-header">
					<view class="header-icon">
						<image src="/static/svg/calendar.svg" class="icon" />
					</view>
					<text class="card-title">时间地点</text>
				</view>

				<view class="form-item">
					<text class="label">活动时间 <text class="required">*</text></text>
					<view class="datetime-container">
						<picker mode="date" @change="onDateChange">
							<view class="datetime-picker date-picker">
								<image src="/static/svg/calendar.svg" class="picker-icon" />
								<text class="picker-text">{{ activityData.date || '选择日期' }}</text>
							</view>
						</picker>
						<picker mode="time" @change="onTimeChange">
							<view class="datetime-picker time-picker">
								<image src="/static/svg/clock.svg" class="picker-icon" />
								<text class="picker-text">{{ activityData.time || '选择时间' }}</text>
							</view>
						</picker>
					</view>
				</view>

				<view class="form-item">
					<text class="label">活动地点 <text class="required">*</text></text>
					<view class="location-input" @click="chooseLocation">
						<image src="/static/svg/map-pin.svg" class="location-icon" />
						<text class="location-text" :class="{ placeholder: !activityData.location }">
							{{ activityData.location || '点击选择活动地点' }}
						</text>
						<image src="/static/svg/chevron-right.svg" class="arrow-icon" />
					</view>
				</view>
			</view>

			<!-- 参与设置 -->
			<view class="form-card">
				<view class="card-header">
					<view class="header-icon">
						<image src="/static/svg/users.svg" class="icon" />
					</view>
					<text class="card-title">参与设置</text>
				</view>

				<view class="form-item">
					<text class="label">参与人数</text>
					<view class="number-selector">
						<view class="number-controls">
							<view class="number-btn" @click="decreaseNumber" :class="{ disabled: activityData.maxParticipants <= 1 }">
								<image src="/static/svg/minus.svg" class="btn-icon" />
							</view>
							<input v-model="activityData.maxParticipants" type="number" class="number-input" min="1" max="999" />
							<view class="number-btn" @click="increaseNumber" :class="{ disabled: activityData.maxParticipants >= 999 }">
								<image src="/static/svg/add.svg" class="btn-icon" />
							</view>
						</view>
						<text class="number-hint">最多999人</text>
					</view>
				</view>

				<view class="form-item">
					<text class="label">报名费用</text>
					<view class="fee-section">
						<view class="fee-toggle">
							<view class="fee-option" :class="{ active: activityData.feeType === 'free' }" @click="setFeeType('free')">
								<image src="/static/svg/gift.svg" class="option-icon" />
								<text>免费</text>
							</view>
							<view class="fee-option" :class="{ active: activityData.feeType === 'paid' }" @click="setFeeType('paid')">
								<image src="/static/svg/credit-card.svg" class="option-icon" />
								<text>付费</text>
							</view>
						</view>
						<view v-if="activityData.feeType === 'paid'" class="fee-input-container">
							<view class="fee-input-wrapper">
								<text class="currency-symbol">¥</text>
								<input v-model="activityData.fee" type="number" placeholder="0.00" class="fee-input" />
								<text class="fee-unit">元/人</text>
							</view>
						</view>
					</view>
				</view>
			</view>

			<!-- 其他设置 -->
			<view class="form-card">
				<view class="card-header">
					<view class="header-icon">
						<image src="/static/svg/settings.svg" class="icon" />
					</view>
					<text class="card-title">其他设置</text>
				</view>

				<view class="switch-item">
					<view class="switch-content">
						<image src="/static/svg/shield-check.svg" class="switch-icon" />
						<view class="switch-text">
							<text class="switch-label">需要审核</text>
							<text class="switch-desc">开启后需要你审核通过才能参与活动</text>
						</view>
					</view>
					<switch :checked="activityData.needApproval" @change="onNeedApprovalChange" color="#3de6e6" />
				</view>

				<view class="switch-item">
					<view class="switch-content">
						<image src="/static/svg/share.svg" class="switch-icon" />
						<view class="switch-text">
							<text class="switch-label">允许转发</text>
							<text class="switch-desc">允许参与者将活动分享给朋友</text>
						</view>
					</view>
					<switch :checked="activityData.allowShare" @change="onAllowShareChange" color="#3de6e6" />
				</view>
			</view>

			<!-- 底部安全距离 -->
			<view class="bottom-safe-area"></view>
		</scroll-view>

		<!-- 底部发布栏 -->
		<view class="publish-footer">
			<view class="footer-content">
				<button class="draft-btn" @click="saveDraft">
					<image src="/static/svg/bookmark.svg" class="btn-icon" />
					<text>存草稿</text>
				</button>
				<button class="publish-btn" @click="publishActivity">
					<image src="/static/svg/send.svg" class="btn-icon" />
					<text>立即发布</text>
				</button>
			</view>
		</view>
	</view>
</template>

<script setup>
import { ref, reactive } from 'vue'

// 活动数据
const activityData = reactive({
	title: '',
	description: '',
	cover: '',
	category: '',
	date: '',
	time: '',
	location: '',
	maxParticipants: 10,
	feeType: 'free',
	fee: '',
	needApproval: false,
	allowShare: true
})

// 活动分类
const categories = ref([
	{ id: 1, name: '运动健身', icon: '/static/svg/walking.svg' },
	{ id: 2, name: '学习成长', icon: '/static/svg/category-growth.svg' },
	{ id: 3, name: '美食聚会', icon: '/static/svg/gourds.svg' },
	{ id: 4, name: '文化艺术', icon: '/static/svg/cherry-blossom.svg' },
	{ id: 5, name: '旅行出游', icon: '/static/svg/landscape.svg' },
	{ id: 6, name: '综合活动', icon: '/static/svg/comprehensive.svg' }
])

const selectedCategory = ref({})

// 选择封面
const chooseCover = () => {
	uni.chooseImage({
		count: 1,
		sizeType: ['compressed'],
		success: (res) => {
			activityData.cover = res.tempFilePaths[0]
		}
	})
}

// 分类选择
const onCategoryChange = (e) => {
	const index = e.detail.value
	selectedCategory.value = categories.value[index]
	activityData.category = selectedCategory.value.id
}

// 日期选择
const onDateChange = (e) => {
	activityData.date = e.detail.value
}

// 时间选择
const onTimeChange = (e) => {
	activityData.time = e.detail.value
}

// 地点选择
const chooseLocation = () => {
	uni.chooseLocation({
		success: (res) => {
			activityData.location = res.address
		}
	})
}

// 人数调整
const decreaseNumber = () => {
	if (activityData.maxParticipants > 1) {
		activityData.maxParticipants--
	}
}

const increaseNumber = () => {
	if (activityData.maxParticipants < 999) {
		activityData.maxParticipants++
	}
}

// 费用类型设置
const setFeeType = (type) => {
	activityData.feeType = type
	if (type === 'free') {
		activityData.fee = ''
	}
}

// 表单验证
const validateForm = () => {
	if (!activityData.title.trim()) {
		uni.showToast({ title: '请输入活动标题', icon: 'none' })
		return false
	}
	if (!activityData.description.trim()) {
		uni.showToast({ title: '请输入活动描述', icon: 'none' })
		return false
	}
	if (!activityData.category) {
		uni.showToast({ title: '请选择活动分类', icon: 'none' })
		return false
	}
	if (!activityData.date) {
		uni.showToast({ title: '请选择活动日期', icon: 'none' })
		return false
	}
	if (!activityData.time) {
		uni.showToast({ title: '请选择活动时间', icon: 'none' })
		return false
	}
	if (!activityData.location) {
		uni.showToast({ title: '请选择活动地点', icon: 'none' })
		return false
	}
	return true
}

// 保存草稿
const saveDraft = () => {
	uni.showLoading({ title: '保存中...' })
	// 模拟保存操作
	setTimeout(() => {
		uni.hideLoading()
		uni.showToast({ title: '草稿已保存', icon: 'success' })
	}, 1000)
}

// switch组件变化处理
const onNeedApprovalChange = (e) => {
	activityData.needApproval = e.detail.value
}

const onAllowShareChange = (e) => {
	activityData.allowShare = e.detail.value
}

// 发布活动
const publishActivity = () => {
	if (!validateForm()) return
	
	uni.showLoading({ title: '发布中...' })
	// 模拟发布操作
	setTimeout(() => {
		uni.hideLoading()
		uni.showToast({ 
			title: '发布成功', 
			icon: 'success',
			success: () => {
				setTimeout(() => {
					uni.navigateBack()
				}, 1500)
			}
		})
	}, 2000)
}
</script>

<style scoped>
.page {
	min-height: 100vh;
	background: linear-gradient(135deg, #d6f6f8 0%, #e8f8f9 100%);
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}



/* 内容容器 */
.content-container {
	flex: 1;
	padding: 20px 16px 0 16px;
	box-sizing: border-box;
	width: 100%;
	max-width: 100vw;
	overflow-x: hidden;
}

/* 封面区域 */
.cover-section {
	margin: 20px 0;
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
}

.cover-container {
	border-radius: 16px;
	overflow: hidden;
	box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
}

.cover-upload {
	position: relative;
	width: 100%;
	height: 200px;
	background: linear-gradient(135deg, #ffffff 0%, #f8f9fa 100%);
}

.cover-preview {
	position: relative;
	width: 100%;
	height: 100%;
}

.cover-image {
	width: 100%;
	height: 100%;
}

.cover-overlay {
	position: absolute;
	top: 0;
	left: 0;
	right: 0;
	bottom: 0;
	background: rgba(0, 0, 0, 0.5);
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	opacity: 0;
	transition: opacity 0.3s ease;
}

.cover-preview:active .cover-overlay {
	opacity: 1;
}

.edit-icon {
	width: 24px;
	height: 24px;
	margin-bottom: 8px;
	filter: brightness(0) invert(1);
}

.edit-text {
	color: white;
	font-size: 14px;
}

.upload-placeholder {
	width: 100%;
	height: 100%;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	padding: 32px;
}

.upload-icon-wrapper {
	width: 64px;
	height: 64px;
	border-radius: 32px;
	background: linear-gradient(135deg, #3de6e6 0%, #2bc9cb 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	margin-bottom: 16px;
	box-shadow: 0 4px 16px rgba(61, 230, 230, 0.3);
}

.camera-icon {
	width: 32px;
	height: 32px;
	filter: brightness(0) invert(1);
}

.upload-title {
	font-size: 18px;
	font-weight: 600;
	color: #222;
	margin-bottom: 8px;
}

.upload-subtitle {
	font-size: 14px;
	color: #666;
	text-align: center;
	line-height: 1.4;
}

/* 表单卡片 */
.form-card {
	background: rgba(255, 255, 255, 0.95);
	border-radius: 16px;
	margin: 0 0 16px 0;
	padding: 20px;
	box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
	border: 1px solid rgba(255, 255, 255, 0.8);
	backdrop-filter: blur(10px);
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
}

.card-header {
	display: flex;
	align-items: center;
	margin-bottom: 20px;
	padding-bottom: 12px;
	border-bottom: 1px solid rgba(61, 230, 230, 0.1);
}

.header-icon {
	width: 32px;
	height: 32px;
	border-radius: 8px;
	background: linear-gradient(135deg, #3de6e6 0%, #2bc9cb 100%);
	display: flex;
	align-items: center;
	justify-content: center;
	margin-right: 12px;
}

.icon {
	width: 18px;
	height: 18px;
	filter: brightness(0) invert(1);
}

.card-title {
	font-size: 16px;
	font-weight: 600;
	color: #222;
}

/* 表单项 */
.form-item {
	margin-bottom: 24px;
}

.form-item:last-child {
	margin-bottom: 0;
}

.label {
	display: block;
	font-size: 15px;
	font-weight: 500;
	color: #222;
	margin-bottom: 12px;
}

.required {
	color: #e74c3c;
	font-weight: 600;
}

/* 输入框样式 */
.input-wrapper, .textarea-wrapper {
	position: relative;
}

.modern-input {
	width: 100%;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 0 16px;
	padding-right: 60px; /* 为字符计数预留空间 */
	font-size: 16px;
	background: white;
	color: #222;
	transition: all 0.3s ease;
	box-sizing: border-box;
}

.modern-input:focus {
	border-color: #3de6e6;
	background: white;
	box-shadow: 0 0 0 3px rgba(61, 230, 230, 0.1);
	outline: none;
}

.modern-textarea {
	width: 100%;
	min-height: 100px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 16px;
	padding-bottom: 40px; /* 为字符计数预留空间 */
	font-size: 16px;
	background: white;
	color: #222;
	resize: none;
	transition: all 0.3s ease;
	line-height: 1.5;
	box-sizing: border-box;
}

.modern-textarea:focus {
	border-color: #3de6e6;
	background: white;
	box-shadow: 0 0 0 3px rgba(61, 230, 230, 0.1);
	outline: none;
}

.char-count {
	position: absolute;
	right: 12px;
	bottom: 12px;
	font-size: 12px;
	color: #666;
	background: rgba(255, 255, 255, 0.95);
	padding: 4px 8px;
	border-radius: 8px;
	border: 1px solid #f0f0f0;
}

/* 选择器样式 */
.modern-picker {
	display: flex;
	align-items: center;
	justify-content: space-between;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 0 16px;
	background: white;
	transition: all 0.3s ease;
	box-sizing: border-box;
}

.modern-picker:active {
	border-color: #3de6e6;
	background: white;
}

.picker-content {
	display: flex;
	align-items: center;
	flex: 1;
	min-width: 0; /* 防止溢出 */
}

.category-icon {
	width: 20px;
	height: 20px;
	margin-right: 8px;
	flex-shrink: 0;
}

.picker-text {
	font-size: 16px;
	color: #222;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.arrow-icon {
	width: 16px;
	height: 16px;
	filter: opacity(0.7);
	flex-shrink: 0;
}

/* 时间选择器 */
.datetime-container {
	display: flex;
	gap: 12px;
}

.datetime-picker {
	flex: 1;
	display: flex;
	align-items: center;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 0 16px;
	background: white;
	transition: all 0.3s ease;
	box-sizing: border-box;
	min-width: 0; /* 防止溢出 */
}

.datetime-picker:active {
	border-color: #3de6e6;
	background: white;
}

.picker-icon {
	width: 18px;
	height: 18px;
	margin-right: 10px;
	filter: opacity(0.7);
	flex-shrink: 0;
}

/* 地点选择器 */
.location-input {
	display: flex;
	align-items: center;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 0 16px;
	background: white;
	transition: all 0.3s ease;
	box-sizing: border-box;
}

.location-input:active {
	border-color: #3de6e6;
	background: white;
}

.location-icon {
	width: 18px;
	height: 18px;
	margin-right: 10px;
	filter: opacity(0.7);
	flex-shrink: 0;
}

.location-text {
	flex: 1;
	font-size: 16px;
	color: #222;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.location-text.placeholder {
	color: #666;
}

/* 数字选择器 */
.number-selector {
	display: flex;
	align-items: center;
	justify-content: space-between;
}

.number-controls {
	display: flex;
	align-items: center;
	gap: 12px;
	background: #f8f9fa;
	border-radius: 12px;
	padding: 4px;
}

.number-btn {
	width: 40px;
	height: 40px;
	border-radius: 8px;
	background: white;
	display: flex;
	align-items: center;
	justify-content: center;
	box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
	transition: all 0.2s ease;
}

.number-btn:active:not(.disabled) {
	transform: scale(0.95);
	background: #3de6e6;
}

.number-btn.disabled {
	opacity: 0.3;
}

.btn-icon {
	width: 16px;
	height: 16px;
}

.number-input {
	width: 80px;
	height: 40px;
	border: none;
	background: white;
	border-radius: 8px;
	text-align: center;
	font-size: 16px;
	font-weight: 600;
	color: #222;
}

.number-hint {
	font-size: 12px;
	color: #666;
}

/* 费用设置 */
.fee-section {
	/* 移除 space-y，使用 margin 替代 */
}

.fee-toggle {
	display: flex;
	gap: 12px;
	margin-bottom: 16px;
}

.fee-option {
	flex: 1;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 8px;
	background: white;
	color: #666;
	transition: all 0.3s ease;
	box-sizing: border-box;
	min-width: 0; /* 防止溢出 */
}

.fee-option.active {
	border-color: #3de6e6;
	background: rgba(61, 230, 230, 0.1);
	color: #3de6e6;
}

.option-icon {
	width: 18px;
	height: 18px;
	flex-shrink: 0;
}

.fee-input-container {
	margin-top: 16px;
}

.fee-input-wrapper {
	display: flex;
	align-items: center;
	height: 48px;
	border: 2px solid #e0e0e0;
	border-radius: 12px;
	padding: 0 16px;
	background: white;
	box-sizing: border-box;
}

.currency-symbol {
	font-size: 18px;
	font-weight: 600;
	color: #222;
	margin-right: 8px;
	flex-shrink: 0;
}

.fee-input {
	flex: 1;
	border: none;
	background: transparent;
	font-size: 16px;
	color: #222;
	min-width: 0; /* 防止溢出 */
}

.fee-input:focus {
	outline: none;
}

.fee-unit {
	font-size: 14px;
	color: #666;
	margin-left: 8px;
	flex-shrink: 0;
}

/* 开关项 */
.switch-item {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 16px 0;
	border-bottom: 1px solid rgba(0, 0, 0, 0.05);
}

.switch-item:last-child {
	border-bottom: none;
	padding-bottom: 0;
}

.switch-content {
	display: flex;
	align-items: flex-start;
	flex: 1;
	margin-right: 16px;
}

.switch-icon {
	width: 20px;
	height: 20px;
	margin-right: 12px;
	margin-top: 2px;
	filter: opacity(0.6);
}

.switch-text {
	flex: 1;
}

.switch-label {
	display: block;
	font-size: 15px;
	font-weight: 500;
	color: #222;
	margin-bottom: 4px;
}

.switch-desc {
	font-size: 13px;
	color: #666;
	line-height: 1.4;
}

/* 底部发布栏 */
.publish-footer {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	background: rgba(255, 255, 255, 0.95);
	backdrop-filter: blur(10px);
	border-top: 1px solid rgba(0, 0, 0, 0.06);
	padding: 12px 16px;
	padding-bottom: calc(12px + var(--safe-area-inset-bottom, 0px));
}

.footer-content {
	display: flex;
	gap: 12px;
	box-sizing: border-box;
	width: 100%;
	max-width: 100%;
}

.draft-btn {
	flex: 1;
	height: 48px;
	border: 2px solid #e5e5e5;
	border-radius: 24px;
	background: white;
	color: #666;
	font-size: 16px;
	font-weight: 500;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
	transition: all 0.3s ease;
}

.draft-btn:active {
	transform: scale(0.98);
	border-color: #3de6e6;
	color: #3de6e6;
}

.publish-btn {
	flex: 2;
	height: 48px;
	background: linear-gradient(135deg, #3de6e6 0%, #2bc9cb 100%);
	border: none;
	border-radius: 24px;
	color: white;
	font-size: 16px;
	font-weight: 600;
	display: flex;
	align-items: center;
	justify-content: center;
	gap: 6px;
	box-shadow: 0 4px 16px rgba(61, 230, 230, 0.3);
	transition: all 0.3s ease;
}

.publish-btn:active {
	transform: scale(0.98);
	box-shadow: 0 2px 8px rgba(61, 230, 230, 0.4);
}

.btn-icon {
	width: 16px;
	height: 16px;
}

.bottom-safe-area {
	height: 120px;
}

/* 页面基础防溢出 */
.page {
	box-sizing: border-box;
	width: 100%;
	max-width: 100vw;
	overflow-x: hidden;
}

/* 响应式调整 */
@media (max-width: 400px) {
	.content-container {
		padding: 16px 12px 0 12px;
	}
	
	.form-card {
		padding: 16px;
		margin: 0 0 16px 0;
	}
	
	.datetime-container {
		flex-direction: column;
		gap: 8px;
	}
	

	
	.modern-input {
		padding-right: 50px; /* 小屏幕上减少右侧padding */
	}
	
	.number-controls {
		gap: 8px;
	}
	
	.number-input {
		width: 60px; /* 小屏幕上减小宽度 */
	}
	
	.publish-footer {
		padding: 12px 12px;
		padding-bottom: calc(12px + var(--safe-area-inset-bottom, 0px));
	}
}

/* 超小屏幕调整 */
@media (max-width: 320px) {
	.content-container {
		padding: 12px 8px 0 8px;
	}
	
	.form-card {
		padding: 12px;
		margin: 0 0 12px 0;
	}
	
	.fee-toggle {
		flex-direction: column;
		gap: 8px;
	}
	
	.modern-input, .modern-textarea {
		font-size: 14px;
	}
	

	
	.publish-footer {
		padding: 12px 8px;
		padding-bottom: calc(12px + var(--safe-area-inset-bottom, 0px));
	}
}
</style> 